home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 050a / tse_mac1.zip / MAKECSV.ZIP / MAKECSV.S < prev   
Text File  |  1993-03-25  |  5KB  |  193 lines

  1. /*  MAKECSV.S   Macro to change a file of records with
  2.     positionally defined fields in records to a file with comma
  3.     separated value fields in records.
  4.     M. W. Hulse, v.1, March 25, 1993
  5.  
  6.     Requires a file named DEFINEcc.RCD where cc can be any two
  7.     characters.  The format of this file is one line for each
  8.     field desired in the output file. Leave no blank lines.  Each
  9.     line is formatted as follows:
  10.  
  11.         Field Name, Beginning Column, Field Length, Character Field (Y/N?),
  12.  
  13.     Where:
  14.  
  15.         Field Name is a single string less than 16 characters. It
  16.         is omisable if not desired in the output, however, if
  17.         omitted for one field, it must be omited for all.
  18.  
  19.         Beginning Column is the starting column of the field.
  20.  
  21.         Field Length is maximum the number of characters in the
  22.         field.
  23.  
  24.         Character Field (Y/N?) indicates whether the field
  25.         contains other than numbers, a leading "-" or a single decimal.
  26.  
  27.     Example:
  28.  
  29.         Last_Name,1,16,Y,
  30.         First_Name,17,16,Y,
  31.         Age,32,3,N,
  32.  
  33.     Fields must be defined in the order they are to appear in the .CSV
  34.     file, but not every field in the input need be used nor do fields
  35.     need to be in the same order as in the source file.
  36.  
  37.     Results are placed in file RESULTcc.CSV where "cc" is the same
  38.     characters as used in the suffix to DEFINEcc.RCD.
  39.  
  40. */
  41.  
  42. // Global Variables
  43.  
  44. String  StrField[1]   = ""
  45.  
  46. Integer StartCol,
  47.         NbrChar,
  48.         DataFile,
  49.         DefineFile,
  50.         ResultFile
  51.  
  52. PROC MoveHeader()                       // move field names into RESULTnn
  53.     String FieldName[16] = ""
  54.  
  55.     Loop
  56.         Markword()
  57.         FieldName = GetMarkedText()
  58.         GotoBufferID(ResultFile)
  59.         EndLine()
  60.         InsertText(Chr(34) + FieldName + Chr(34) + ",", _INSERT_)
  61.         GotoBufferID(DefineFile)
  62.         BegLine()
  63.         If NOT Down() OR CurrChar() < 0
  64.             GotoBufferID(ResultFile)
  65.             EndLine()
  66.             CReturn()
  67.            Break
  68.         EndIf
  69.     EndLoop
  70. END
  71.  
  72. PROC GetFormat()                        // in DEFINEnn file
  73.  
  74.     Lfind(",", "")
  75.     Right()
  76.     Markword()
  77.     StartCol = Val(GetMarkedText())
  78.     Lfind(",", "")
  79.     Right()
  80.     Markword()
  81.     NbrChar = Val(GetMarkedText())
  82.     Lfind(",", "")
  83.     Right()
  84.     Markword()
  85.     StrField = GetMarkedText()
  86. END
  87.  
  88. String PROC ProcessRecord()             // get a field
  89.     String  Text[255] = ""
  90.  
  91.     GotoColumn(StartCol)                // kill leading blanks
  92.     While CurrChar() == 32
  93.         StartCol = StartCol + 1
  94.         NbrChar  = NbrChar  - 1
  95.         Right()
  96.     EndWhile
  97.  
  98.     GotoColumn(StartCol + NbrChar - 1)  // kill trailing blanks
  99.  
  100.     While CurrChar() == 32
  101.         NbrChar  = NbrChar  - 1
  102.         Left()
  103.     EndWhile
  104.  
  105.     Text = GetText(StartCol, NbrChar)           // get the trimed field
  106.     If StrField == "Y"                          // if string
  107.         Text = Chr(34) + Text + Chr(34) + ","   // put quotes
  108.     Else                                        // otherwise
  109.         Text = Text + ","                       // no quotes
  110.     EndIf
  111.     Return(Text)
  112. END
  113.  
  114. PROC MoveData()                         // move fields into RESULTnn
  115.     String Text[255]     = ""
  116.  
  117. // Loop through input file to place records in result file
  118.    Repeat
  119.         GotoBufferID(DefineFile)
  120.         BegFile()
  121.  
  122.         Repeat                          // loop through the DEFINEnn file
  123.             GetFormat()
  124.             GotoBufferID(DataFile)
  125.             Text = ProcessRecord()
  126.  
  127.             GotoBufferID(ResultFile)
  128.             EndLine()
  129.             InsertText(Text, _INSERT_)
  130.             GotoBufferID(DefineFile)
  131.             BegLine()
  132.         Until NOT Down() OR CurrChar() < 0
  133.         GotoBufferID(ResultFile)
  134.         CReturn()
  135.         GotoBufferID(DataFile)
  136.         BegLine()
  137.         Message("Line: ", Currline(), " done...")
  138.     Until NOT Down()
  139.  
  140.  
  141. END
  142.  
  143. PROC MakeCSV()
  144.  
  145.     STRING  fn[2]       = "",
  146.             Q[1]        = "Y"
  147.  
  148.     Set(Break, On)
  149.     DataFile    = GetBufferID()
  150.  
  151. // Can file be processed?
  152.     If lFind('"', "G")
  153.         Message("Double quote(s) in file...")
  154.         Ask("Convert to single quote(s) (Y/N)", Q)
  155.         If Q == "Y"
  156.             lReplace(Chr(34), "'", "G")
  157.         Else
  158.             Message("Abandoning macro while you fix the file...")
  159.             Halt
  160.         EndIf
  161.    EndIf
  162.     // Intialize
  163. // Get and load DEFINEcc.RCD file
  164.  
  165.     If NOT Ask("Enter 2 character DEFINEcc suffix: ", fn)
  166.         Halt
  167.     EndIf
  168.     EditFile("DEFINE" + fn + ".RCD")
  169.     DefineFile = GetBufferID()
  170.  
  171. // Setup output file
  172.     EditFile("RESULT" + fn + ".CSV")
  173.     ResultFile = GetBufferID()
  174.  
  175. // Loop through record to place header in result file
  176.  
  177.     GotoBufferID(DefineFile)
  178.     BegFile()
  179.     If CurrChar() <> 44             // if first character is not a comma
  180.         MoveHeader()
  181.     EndIf
  182.     GotoBufferID(DataFile)
  183.     BegLine()
  184.     MoveData()                      // loop through input file
  185.     GotoBufferID(ResultFile)
  186.     SaveFile()                      // save result file
  187.     AbandonFile()                   // and get out of
  188.     AbandonFile()                   // ...all
  189.     AbandonFile()                   // ...files
  190. END
  191.  
  192. <Alt i> MakeCSV()
  193.